package com.jt.www.admin.order_pc.service.impl;

import com.jt.www.admin.order_pc.service.ExportConfigService;
import com.jt.www.dao.mapper.application.ExportConfigMapper;
import com.jt.www.domain.po.ExportConfigEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * excel 导出配置
 * Created by shaoyn on 2020/06/19.
 */
@Service
public class ExportConfigServiceImpl implements ExportConfigService {

    @Autowired
    private ExportConfigMapper exportConfigMapper;

    @Override
    public ExportConfigEntity getExportConfig(String channelType, String productCode) {

        List<ExportConfigEntity> entities = null;

        Example example = new Example(ExportConfigEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted", false);
        criteria.andEqualTo("channelType", channelType);

        //根据产品编码查询
        if (StringUtils.isNotBlank(productCode)) {
            criteria.andLike("productCodes", "%" + productCode + "%");

            entities = exportConfigMapper.selectByExample(example);
            if (entities != null && entities.size() > 0) {
                return entities.get(0);
            } else {

                Example example1 = new Example(ExportConfigEntity.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("isDeleted", false);
                criteria1.andEqualTo("channelType", channelType);
                criteria1.andEqualTo("productCodes", "default");
                entities = exportConfigMapper.selectByExample(example1);
                if (entities != null && entities.size() > 0) {
                    return entities.get(0);
                }
            }
        } else {
            criteria.andEqualTo("productCodes", "default");
        }

        entities = exportConfigMapper.selectByExample(example);
        if (entities != null && entities.size() > 0) {
            return entities.get(0);
        }
        return null;
    }

    @Override
    public ExportConfigEntity getExportConfig(String channelType) {
        return getExportConfig(channelType, null);
    }

}
